package com.sunday.common.trace.skywalking.study.dubbo;

import com.sunday.common.core.constants.LogConstants;
import com.sunday.common.core.remoting.base.page.PageResultDTO;
import com.sunday.common.core.remoting.base.response.Response;
import com.sunday.common.core.remoting.rpc.response.RPCResponseHandler;
import com.sunday.common.trace.skywalking.study.dubbo.dto.PageUserRoleReqDTO;
import com.sunday.common.trace.skywalking.study.dubbo.dto.PageUserRoleResDTO;
import com.sunday.common.trace.skywalking.study.dubbo.dto.SaveUserRoleReqDTO;
import com.sunday.common.trace.skywalking.study.dubbo.provider.UserRoleProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user/role")
public class UserRoleConsumer {

    @DubboReference
    private UserRoleProvider userRoleProvider;

    @GetMapping("/save")
    public Response save() {

        page();

        SaveUserRoleReqDTO saveUserRoleReqDTO = new SaveUserRoleReqDTO();
        saveUserRoleReqDTO.setRoleId(222L);
        saveUserRoleReqDTO.setUserId(333L);
        log.info(LogConstants.request_format, saveUserRoleReqDTO);
        Response response = RPCResponseHandler.invoke(() -> userRoleProvider.save(saveUserRoleReqDTO));
        log.info(LogConstants.response_format_ack, response);
        return response;
    }

    @GetMapping("/page")
    public Response page() {
        PageUserRoleReqDTO pageUserRoleReqDTO = PageUserRoleReqDTO.of(1, 5);
        log.info(LogConstants.request_format, pageUserRoleReqDTO);
        Response<PageResultDTO<PageUserRoleResDTO>> response = RPCResponseHandler.invoke(() -> userRoleProvider.page(pageUserRoleReqDTO));
        log.info(LogConstants.response_format_ack, response);
        return response;
    }

    @GetMapping("/thread")
    public Response thread() {
        log.info(LogConstants.request_format);
        Response<PageResultDTO<PageUserRoleResDTO>> response = RPCResponseHandler.invoke(() -> userRoleProvider.thread());
        log.info(LogConstants.response_format_ack, response);
        return response;
    }

    @GetMapping("/timeout/{seconds}")
    public Response timeout(@PathVariable Long seconds) {
        log.info(LogConstants.request_format, seconds);
        Response<PageResultDTO<PageUserRoleResDTO>> response = RPCResponseHandler.invoke(() -> userRoleProvider.timeout(seconds));
        log.info(LogConstants.response_format_ack, response);
        return response;
    }

    /**
     * 务必需要 @RequestBody 才可以进行解析
     */
    @PostMapping("/webhooks")
    public Response webhooks(@RequestBody String webhooks) {
        log.info(LogConstants.request_format, webhooks);
        log.info("webhooks 告警是否成功的测试");
        return Response.ok();
    }

    @GetMapping("/trace/{message}")
    public Response trace(@PathVariable String message) {
        log.info(LogConstants.request_format, message);
        Response response = RPCResponseHandler.invoke(() -> userRoleProvider.trace(message));
        log.info(LogConstants.response_format_ack, response);
        return response;
    }

}